export interface Topic {
  id: string
  name: string
  description: string
}

export interface User {
  id: string
  name: string
}

export interface DataItem {
  id: string
  scene: string
  topic: Topic[]
  creator: User
  date: string
}

export interface TreeNode {
  id: string
  label: string
  children: DataItem[]
}

export function classifyByTopic(data: DataItem[]): TreeNode[] | undefined {
  console.log(data)
  if (data === undefined) {
    return undefined
  }else if (data.length === 0) {
    return []
  }
  const map = new Map<string, { topicName: string; items: DataItem[] }>()

  data.forEach((item) => {
    item.topic.forEach((topic) => {
      if (!map.has(topic.id)) {
        map.set(topic.id, { topicName: topic.name, items: [] })
      }
      map.get(topic.id)?.items.push(item)
    })
  })

  const result: TreeNode[] = []
  map.forEach((value, topicId) => {
    result.push({
      id: topicId,
      label: value.topicName,
      children: value.items,
    })
  })

  return result
}


export function classifyByCreator(data: DataItem[]): TreeNode[] | undefined {
  if (data.length === 0) {
    return []
  }else if (data === undefined) {
    return undefined
  }
  const map = new Map<string, DataItem[]>()

  data.forEach((item) => {
    if (!map.has(item.creator.id)) {
      map.set(item.creator.name, [])
    }
    map.get(item.creator.name)?.push(item)
  })

  const result: TreeNode[] = []
  map.forEach((children, creator) => {
    result.push({
      id: creator,
      label: creator,
      children,
    })
  })

  return result
}
